<script setup lang="ts">
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import skeleton from './components/skeleton.vue'
import { ref } from 'vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem, XtxGuessInstance } from '@/types/home';
import { onLoad } from '@dcloudio/uni-app';


//获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
    const res = await getHomeBannerAPI()
    bannerList.value = res.result
}

//获取前台分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
    const res = await getHomeCategoryAPI()
    categoryList.value = res.result
}

//获取前台推荐专区数据
const hotItemlList = ref<HotItem[]>([])
const getHotItemData = async () => {
    const res = await getHomeHotAPI()
    hotItemlList.value = res.result
}

// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 滚动触底事件
const onScrolltolower = () => {
    guessRef.value?.getMore()
}

//下拉刷新动画显示
const isTriggered = ref(false)

//下拉刷新
const onRefresherrefresh = async () => {
    //下拉刷新动画开启
    isTriggered.value = true
    guessRef.value?.resetData()
    //同时调用三个函数
    await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotItemData(), guessRef.value?.getMore()])
    //下拉刷新动画结束
    isTriggered.value = false
}

//骨架屏
const isLoading = ref(false)

//页面加载
onLoad(async () => {
    isLoading.value = true
    await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotItemData()])
    isLoading.value = false
})
</script>

<template>
    <CustomNavbar></CustomNavbar>
    <scroll-view class="scrollView" @scrolltolower="onScrolltolower" refresher-enabled
        @refresherrefresh="onRefresherrefresh" :refresher-triggered="isTriggered" scroll-y>
        <skeleton v-if="isLoading"></skeleton>
        <template v-else>
            <XtxSwiper :list="bannerList"></XtxSwiper>
            <CategoryPanel :list="categoryList"></CategoryPanel>
            <HotPanel :list="hotItemlList"></HotPanel>
            <XtxGuess ref="guessRef"></XtxGuess>
        </template>
    </scroll-view>
</template>

<style lang="scss">
page {
    background-color: #f7f7f7;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.scrollView {
    flex: 1;
}
</style>
